Skip to content

Keep comments above a leading-dot call in a chain - #554

Open
mscrivo wants to merge 2 commits into
ruby-syntax-tree:mainfrom
affinity:fix-leading-dot-comment-chain-main
Open

mscrivo wants to merge 2 commits into
ruby-syntax-tree:mainfrom
affinity:fix-leading-dot-comment-chain-main

Conversation

@mscrivo

@mscrivo mscrivo commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #411.

Since 6.2.0, the formatter breaks a comment on its own line before a leading-dot call in a method chain. It joins the comment to the previous call and moves the dot to a line of its own:

# input, and output in 6.1.1
foo
  .bar(:a, :b)
  # comment
  .baz

# output in 6.2.0 and 6.3.0
foo
  .bar(:a, :b)# comment
  .
  baz

The parser attaches that comment to the call operator. If the operator has any comment, a check that b345765 added to format_chain prints the operator trailing. For a comment after the operator, a trailing operator is necessary, as in the existing foo.\n # comment\n qux fixture. For a comment before a leading dot, the trailing operator breaks the output.

This change applies the check only to an operator comment that starts after the operator ends. A comment before a leading dot then formats as it did in 6.1.1. The trailing-dot fixtures do not change.

The PR adds two fixtures to test/fixtures/call.rb: one comment and two comments before a leading-dot call. Both fail without the change. The formatting and idempotency tests pass. I also formatted a codebase of about 12,000 Ruby files with 6.1.1 and with this change. The only differences were the intended do/end lambda change from 6.2.0.

I saw #472, which was closed because of the move to the prism parser. This is a small, local change for the 6.x line, in case another 6.x release happens.

mscrivo and others added 2 commits November 18, 2026 11:31
Since 6.2.0, a comment on its own line before a leading-dot call in a
method chain is attached to the call operator. format_chain then prints
the operator trailing, which joins the comment to the previous line and
leaves the dot on a line of its own:

    foo
      .bar(:a, :b)# comment
      .
      baz

This is upstream syntax_tree issue 411.

The trailing operator is only necessary when the comment follows the
operator. The check now looks at the comment location, so a comment
that comes before a leading dot stays on its own line, as in 6.1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comments in long call chains regressed in 6.2.0

1 participant